package com.mcxx.modules.miniSalvation.salary.service.read;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.family.service.read.FamilyMemberReadService;
import com.mcxx.modules.miniSalvation.order.dao.read.OrderReadDao;
import com.mcxx.modules.miniSalvation.order.dto.OrderSalaryDTO;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.miniSalvation.order.service.read.StandardReadService;
import com.mcxx.modules.miniSalvation.salary.dao.read.FamilySalaryReadDao;
import com.mcxx.modules.miniSalvation.salary.dao.read.MemberSalaryReadDao;
import com.mcxx.modules.miniSalvation.salary.dto.MemberSalaryDTO;
import com.mcxx.modules.miniSalvation.salary.entity.FamilySalaryEntity;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import dm.jdbc.util.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("familySalaryReadService")
@TaTransactional(readOnly = true)
public class FamilySalaryReadService {
    @Autowired
    private FamilySalaryReadDao familySalaryReadDao;
    @Autowired
    private MemberSalaryReadDao memberSalaryReadDao;
    @Autowired
    private OrderReadDao orderReadDao;
    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private FamilyMemberReadService familyMemberReadService;
    @Autowired
    private StandardReadService standardReadService;


    public  FamilySalaryEntity getFamilySumSalaryByOrderId(String id){
        return  familySalaryReadDao.getFamilySumSalaryByOrderId(id);
    }


    public List<FamilySalaryEntity> findFamilySalaryByOrderId(String orderId){
        return familySalaryReadDao.findFamilySalaryByOrderId(orderId);
    }

    public double getTotalFeeByOrderId(String orderId, String curMonth) {
        return familySalaryReadDao.getTotalFeeByOrderId(orderId,curMonth);
    }


    public String getStatDateByOrderId(String orderId) {
        return familySalaryReadDao.getStatDateByOrderId(orderId);
    }


    public Map<String, Object> getFamilySalaryInfo(String orderId,String sendMonth, UserAccountVo userAccountVo){
        Map<String, Object> rstMap = new HashMap<>();

        OrderSalaryDTO orderSalary = orderReadDao.getSalaryInfo(orderId);

        if(!CommonUtil.invalidArgs(userAccountVo)){

            String standardMoney = standardReadService.getStandardMoney(userAccountVo.getAreaCode().substring(0, 6)+ "000000", orderSalary.getBusinessType(), orderSalary.getStandType());
            orderSalary.setStandardMoney(standardMoney);
        }



        List<MemberSalaryDTO> memberSalaryList;
        String state = orderReadService.getStateById(orderId);

        if (!Constant.HIS_ABLE_STATUS.contains(state)) {
            memberSalaryList = memberSalaryReadDao.listMemberSalary(orderSalary.getFamilyId(), orderId, null);
        } else {
            memberSalaryList = memberSalaryReadDao.listMemberSalaryHistory(orderId,sendMonth);
            Double  salarySum;
            if(Constant.BusinessCode.DIBAO.equals(orderSalary.getBusinessType())){
                salarySum = memberSalaryList.stream().mapToDouble(o-> o.getBasicSalary()==null?0d:o.getBasicSalary()).sum()+ memberSalaryList.stream().mapToDouble(o-> o.getSortSalary()==null?0d:o.getSortSalary()).sum();
            }else{
                salarySum = memberSalaryList.stream().mapToDouble(o-> o.getLivingSalary()==null?0d:o.getLivingSalary()).sum()+ memberSalaryList.stream().mapToDouble(o-> o.getNurseSalary()==null?0d:o.getNurseSalary()).sum();

            }
            orderSalary.setSalvMoney(new BigDecimal(salarySum).setScale(2, RoundingMode.HALF_UP).doubleValue());
        }
        rstMap.put("orderSalary", orderSalary);
        rstMap.put("memberSalaryList", memberSalaryList);

        return  rstMap;
    }


    public Map<String, Object> getRecheckFamilySalaryInfo(String orderId,UserAccountVo userAccountVo){

        Map<String, Object> rstMap = new HashMap<>();

        OrderSalaryDTO orderSalary = orderReadDao.getSalaryInfo(orderId);
        rstMap.put("orderSalary", orderSalary);
        if(Constant.SaveType.DRB.equals(orderSalary.getFamilyObjectType()) && !CommonUtil.invalidArgs(userAccountVo)){

            String standardMoney = standardReadService.getStandardMoney(userAccountVo.getAreaCode().substring(0, 6)+ "000000", orderSalary.getBusinessType(), orderSalary.getStandType());
            orderSalary.setStandardMoney(standardMoney);
        }

        List<MemberSalaryDTO> memberSalaryList;
        String state = orderReadService.getStateById(orderId);

        if (!Constant.HIS_ABLE_STATUS.contains(state)) {

            int count = familyMemberReadService.isHouseholdCancel(orderId);


//                memberSalaryList = memberSalaryReadDao.listMemberSalaryHouseholdSuspend(orderSalary.getFamilyId(), orderId, null);

                memberSalaryList = memberSalaryReadDao.listMemberSalaryRecheck(orderSalary.getFamilyId(), orderId, null);

        } else {
            memberSalaryList = memberSalaryReadDao.listMemberSalaryHistory(orderId,null);
        }
        rstMap.put("memberSalaryList", memberSalaryList);

        return  rstMap;
    }



    public void checkFamilyIsSaved(String orderId, String familyId, String startDate) {
        Integer existFamilySalary = familySalaryReadDao.getFamilySalaryExistByOrderId(familyId, startDate, orderId);
        if (existFamilySalary > 0) {
            throw new BizException("-1","该家庭正在享受待遇，请在停保生效后再审核确认。");
        }

    }


    public Integer selectIsSavedByIdcard(@Param("idcard") String idcard, @Param("curMonth")String curMonth){
        return familySalaryReadDao.selectIsSavedByIdcard(idcard,curMonth);
    }

   public Integer selectCountByOrderId(String orderId){
       return familySalaryReadDao.selectCountByOrderId(orderId);
   }


    public String getFamilySalaryMaxDate(String orderId, String familyId, String businessType){
        return familySalaryReadDao.getFamilySalaryMaxDate(orderId, familyId, businessType);
    }


    public String getApplyDateByOrderId(String orderId){
        return  this.getEntityBySourceId(orderId);
    }


    private String getEntityBySourceId(String orderId) {
        OrderEntity entity =  orderReadDao.getStartDateBySourceId(orderId);
        if(entity==null) {
            return "";
        }else if(StringUtil.isEmpty(entity.getSourOrderId())){
            return entity.getStartMonth();
        } else {
            return this.getEntityBySourceId(entity.getOrderId());
        }
    }
}
